package com.zhuiyun.project.api.messagelist.mapper;

import com.zhuiyun.project.api.groupmessage.entity.GroupMessage;
import com.zhuiyun.project.api.messagelist.entity.MessageList;
import com.zhuiyun.project.common.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * @ClassName MessageListMapper
 * @Description TODO
 * @Author kdj
 * @Date 2023/6/28 14:35
 **/
@Mapper
public interface MessageListMapper extends BaseMapper<MessageList> {
    /**
     * @param groupMessage
     * @Author kdj
     * @Description 批量更新群的消息列表
     * @Date 2023/7/29 20:06
     * @Return void
     */
    @Update("UPDATE message_list " +
            "SET un_read_num = CASE " +
            "    WHEN to_id = #{fromId} THEN 0 " +
            "    ELSE un_read_num + 1 " +
            "    END, " +
            "    last_message_id = #{messageId}, " +
            "    last_message_content = #{content}, " +
            "    last_message_time = #{createTime} " +
            "WHERE (from_id = #{toId} AND type = 2 and del_flag=0)  ")
    int upGroupMsgLists(GroupMessage groupMessage);

    /**
     * @param lastMessageId
     * @param lastMessageContent
     * @param lastMessageTime
     * @Author kdj
     * @Description 修改最后消息
     * @Date 2023/7/31 12:04
     * @Return void
     */
    @Update("update message_list set last_message_id=#{lastMessageId}, last_message_content=#{lastMessageContent}" +
            ",last_message_time=#{lastMessageTime} where id=#{msgListId} and del_flag=0")
    int upListLast(Integer lastMessageId, String lastMessageContent, Date lastMessageTime, int msgListId);

    /**
     * @param toId
     * @param fromId
     * @param userType
     * @param lastMessageContent
     * @Author kdj
     * @Description 群聊撤回消息之后的其他用户的list
     * @Date 2023/7/31 17:12
     * @Return void
     */
    @Update("update message_list set last_message_content=#{lastMessageContent}" +
            " where from_id=#{toId} and  to_id!=#{fromId} and type=2  and del_flag=0")
    void upMsgGroupListByRecallOther(Integer toId, Integer fromId, Integer userType, String lastMessageContent);


    /**
     * @param toId
     * @param fromId
     * @param userType
     * @param lastMessageContent
     * @Author kdj
     * @Description 群聊撤回消息之后的当前的list
     * @Date 2023/7/31 17:12
     * @Return void
     */
    @Update("update message_list set last_message_content=#{lastMessageContent}" +
            " where from_id=#{toId} and  to_id=#{fromId} and type=2 and del_flag=0")
    void upMsgGroupListByRecallMe(Integer toId, Integer fromId, Integer userType, String lastMessageContent);

    /**
     * @param fromId
     * @param groupId
     * @Author kdj
     * @Description 提出群聊 修改删除表示
     * @Date 2023/8/8 19:14
     * @Return int
     */
    @Update("update message_list set del_flag = 1" +
            " where from_id=#{groupId} and  to_id=#{userId} and type=2")
    int masslistDel(Integer userId, Integer groupId);

    /**
     * @param fromId
     * @param groupId
     * @Author kdj
     * @Description 提出群聊 修改删除表示
     * @Date 2023/8/8 19:14
     * @Return int
     */

    @Update("update message_list set del_flag = 1" +
            " where from_id=#{groupId} and  to_id=#{userId}")
    int masslistDels(Integer userId, Integer groupId);


    /**
    * @Author kdj
    * @Description 获取消息列表
    * @Date 2023/8/12 12:02
    * @Return java.util.List<com.zhuiyun.project.api.messagelist.entity.MessageList>
    * @param userId
    */
    @Select("SELECT IF(type =1,a.image_id,from_avatar_id) as from_avatar_id,m.id,from_id,to_id,un_read_num,last_message_id,last_message_content,last_message_time,type,\n" +
            " mushin_time,list_name,m.create_time,m.update_time,m.del_flag,is_muted \n" +
            "\n" +
            "FROM message_list  m\n" +
            "left join app_user a on a.id=m.from_id and type=1\n" +
            "WHERE ( ( to_id = #{userId} and m.del_flag = 0 ) ) order by last_message_time DESC \n")
    List<MessageList> getMsgList(int userId);
}
